function cs_pso_optimization_ti()
    % CSPSO参数设置
    n_particles = 100;     % 粒子数量
    max_iter = 50;         % 最大迭代次数
    w = 0.7;               % 惯性权重
    c1 = 1.5;              % 个体学习因子
    c2 = 1.5;              % 群体学习因子
    pc_max = 0.8;          % 最大交叉概率
    pc_min = 0.2;          % 最小交叉概率
    elite_ratio = 0.1;     % 精英保留比例
    
    % 变量边界
    lb = [0.05; 0.01; 0.01; 0.01;0.05];
    ub = [0.2; 0.2; 0.06; 0.04;0.4];
    dim = length(lb);      % 变量维度
    
    % 初始化粒子群
    particles = zeros(n_particles, dim);
    velocities = zeros(n_particles, dim);
    pbest_pos = zeros(n_particles, dim);
    pbest_val = inf(n_particles, 1);
    gbest_pos = zeros(1, dim);
    gbest_val = inf;
    
    % 记录收敛过程
    convergence = zeros(max_iter, 1);
    diversity = zeros(max_iter, 1);  % 记录种群多样性
    
    % 初始化粒子位置和速度
    for i = 1:n_particles
        particles(i,:) = lb' + (ub' - lb') .* rand(1, dim);
        velocities(i,:) = 0.1 * (ub' - lb') .* randn(1, dim);
        pbest_pos(i,:) = particles(i,:);
        
        % 计算初始适应度
        current_val = hmm_ti(particles(i,:));%
        pbest_val(i) = current_val;
        
        % 更新全局最优
        if current_val < gbest_val
            gbest_val = current_val;
            gbest_pos = particles(i,:);
        end
    end

    % CSPSO主循环
    for iter = 1:max_iter
        % 在每次迭代结束时添加以下代码
        if iter > 5 && diversity(iter)/diversity(1) < 0.3
            % 多样性紧急恢复机制
            n_reset = ceil(n_particles*0.3);
            reset_idx = randperm(n_particles, n_reset);

            for idx = reset_idx
                if rand() > 0.2  % 80%概率完全重置，20%概率在最优解附近搜索
                    new_pos = lb' + (ub'-lb').*rand(1,dim);
                else
                    new_pos = gbest_pos .* (0.9 + 0.2*rand(1,dim));
                    new_pos = min(max(new_pos, lb'), ub');
                end

                new_val = hmm_ti(new_pos);%
                particles(idx,:) = new_pos;
                pbest_pos(idx,:) = new_pos;
                pbest_val(idx) = new_val;

                if new_val < gbest_val
                    gbest_val = new_val;
                    gbest_pos = new_pos;
                end
            end
            disp(['触发多样性重置 at iter=' num2str(iter)]);
        end
                % 计算当前种群多样性(用于自适应交叉概率)
        pop_mean = mean(particles);
        current_diversity = mean(sqrt(sum((particles - pop_mean).^2, 2)));
        diversity(iter) = current_diversity;
        
        % 自适应交叉概率
        pc = pc_max - (pc_max - pc_min) * (iter/max_iter);
        
        % 标准PSO更新
        for i = 1:n_particles
            % 更新速度
            r1 = rand(1, dim);
            r2 = rand(1, dim);
            velocities(i,:) = w * velocities(i,:) + ...
                             c1 * r1 .* (pbest_pos(i,:) - particles(i,:)) + ...
                             c2 * r2 .* (gbest_pos - particles(i,:));
            
            % 限制速度范围
            max_velocity = 0.2 * (ub' - lb');
            velocities(i,:) = min(max(velocities(i,:), -max_velocity), max_velocity);
            
            % 更新位置
            particles(i,:) = particles(i,:) + velocities(i,:);
            
            % 确保位置在边界内
            particles(i,:) = min(max(particles(i,:), lb'), ub');
            
            % 计算适应度
            current_val = hmm_ti(particles(i,:));%
            
            % 更新个体最优
            if current_val < pbest_val(i)
                pbest_val(i) = current_val;
                pbest_pos(i,:) = particles(i,:);
                
                % 更新全局最优
                if current_val < gbest_val
                    gbest_val = current_val;
                    gbest_pos = particles(i,:);
                end
            end
        end
        
        % 交叉操作(CSPSO核心改进)
        [particles, pbest_pos, pbest_val] = crossover_operation(...
            particles, pbest_pos, pbest_val, pc, elite_ratio, lb, ub);
        
        % 更新全局最优(交叉后可能需要更新)
        [min_val, min_idx] = min(pbest_val);
        if min_val < gbest_val
            gbest_val = min_val;
            gbest_pos = pbest_pos(min_idx,:);
        end
        
        % 记录当前最优值
        convergence(iter) = gbest_val;
        
        % 显示迭代信息
        fprintf('Iter %d: Best=%.4f, Diversity=%.4f, Pc=%.2f\n', ...
                iter, gbest_val, current_diversity, pc);
        
        % 绘制收敛曲线
        figure(1);
        subplot(2,1,1);
        plot(1:iter, -convergence(1:iter), 'b-', 'LineWidth', 2);
        xlabel('Iteration');
        ylabel('Best Absorption');
        title('CSPSO Convergence');
        grid on;
        
        subplot(2,1,2);
        plot(1:iter, diversity(1:iter), 'r-', 'LineWidth', 2);
        xlabel('Iteration');
        ylabel('Population Diversity');
        grid on;
        drawnow;
        % 在绘图部分增加以下监控
        figure(2);
        subplot(3,1,1);
        plot(particles', 'LineWidth',0.5); 
        title('粒子位置分布');
        
        subplot(3,1,2);
        histogram(pbest_val, 20);
        title('适应度值分布');
        
        subplot(3,1,3);
        plot(velocities', 'LineWidth',0.5);
        title('粒子速度分布');
    end
    
    % 显示最终结果
    fprintf('\nOptimization completed!\n');
    fprintf('Best thickness found:\n');
    fprintf('t1 = %.5f um\n', gbest_pos(1));
    fprintf('t2 = %.5f um\n', gbest_pos(2));
    fprintf('t3 = %.5f um\n', gbest_pos(3));
    fprintf('t4 = %.5f um\n', gbest_pos(4));
    fprintf('t5 = %.5f um\n', gbest_pos(5));
    fprintf('Best absorption = %.2f%%\n', -gbest_val*100);
    
    % 保存结果
    save('cs_pso_results_ti_3.mat', 'gbest_pos', 'gbest_val', 'convergence', 'diversity');
end

% 交叉操作函数
function [new_particles, new_pbest_pos, new_pbest_val] = ...
    crossover_operation(particles, pbest_pos, pbest_val, pc, elite_ratio, lb, ub)
    
    n_particles = size(particles, 1);
    dim = size(particles, 2);
    
    % 精英保留
    n_elite = round(n_particles * elite_ratio);
    [~, elite_idx] = sort(pbest_val);
    elite_idx = elite_idx(1:n_elite);
    
    % 初始化新种群
    new_particles = particles;
    new_pbest_pos = pbest_pos;
    new_pbest_val = pbest_val;
    
    % 对非精英粒子进行交叉
    for i = 1:n_particles
        if ~ismember(i, elite_idx) && rand() < pc
            % 随机选择交叉伴侣(不能是自己)
            j = randi([1, n_particles]);
            while j == i
                j = randi([1, n_particles]);
            end
            
            % 算术交叉(产生两个后代)
            alpha = rand(1, dim);  % 随机混合系数
            child1 = alpha .* particles(i,:) + (1-alpha) .* particles(j,:);
            child2 = alpha .* particles(j,:) + (1-alpha) .* particles(i,:);
            
            % 边界处理
            child1 = min(max(child1, lb'), ub');
            child2 = min(max(child2, lb'), ub');
            
            % 评估后代适应度
            val1 = hmm_ti(child1);%
            val2 = hmm_ti(child2);%
            
            % 替换原粒子(保留较好的后代)
            if val1 < val2
                new_particles(i,:) = child1;
                new_pbest_pos(i,:) = child1;
                new_pbest_val(i) = val1;
            else
                new_particles(i,:) = child2;
                new_pbest_pos(i,:) = child2;
                new_pbest_val(i) = val2;
            end
        end
    end
end